package  
{
	import bulletbehavior.AccelerateRelativeBehavior;
	import bulletbehavior.AccelerateXYBehavior;
	import bulletbehavior.AdjustSizeBehavior;
	import bulletbehavior.DieBehavior;
	import bulletbehavior.SplitBehavior;
	import bulletbehavior.WaveBehavior;
	import turretbehavior.SpinningTurretBehavior;
	import turretbehavior.TypeCyclingTurretBehavior;
	import org.flixel.FlxG;
	/**
	 * ...
	 * @author morgan
	 */
	public class Stage4State extends GameState
	{
		[Embed (source = "../data/tutorial_candy.png")] private var img_tutcandy:Class;
		public function Stage4State() 
		{
			super();
			levelName = "2-1";
			levelIndex = 3;
			chefName = "sir bisque";
			img_terrain = img_terrain_2;
		}
		
		override public function setupBoss():void 
		{
			boss = new Boss(FlxG.width / 2 - 70, 10, 1);
			
			var straight:BulletPattern = new BulletPattern();			
			var gravity:BulletPattern = new BulletPattern();
			gravity.addBehavior(new AccelerateXYBehavior(0, 150));
			var reverseGravity:BulletPattern = new BulletPattern();
			reverseGravity.addBehavior(new AccelerateXYBehavior(0, -63));
			var stopAndDie:BulletPattern = new BulletPattern();
			stopAndDie.addBehavior(new AccelerateRelativeBehavior( -100, 0));
			stopAndDie.addBehavior(new DieBehavior(), 1);
			
			var lineSplit:BulletPattern = new BulletPattern();
			lineSplit.addBehavior(new SplitBehavior(1, 80, -50, 0, straight, 0), 0.5);
			lineSplit.addBehavior(new SplitBehavior(1, 80, -50, 0, straight, 1), 1.0);
			lineSplit.addBehavior(new SplitBehavior(1, 80, -50, 0, straight, 2), 1.5);
			lineSplit.addBehavior(new SplitBehavior(1, 80, -50, 0, straight, 0), 2.0);
			lineSplit.addBehavior(new SplitBehavior(1, 80, -50, 0, straight, 1), 2.5);
			lineSplit.addBehavior(new SplitBehavior(1, 80, -50, 0, straight, 2), 3.0);
			lineSplit.addBehavior(new SplitBehavior(1, 80, -50, 0, straight, 0), 3.5);
			lineSplit.addBehavior(new AdjustSizeBehavior(2));
			
			var lineSplit2:BulletPattern = new BulletPattern();
			lineSplit2.addBehavior(new SplitBehavior(1, 80, 50, 0, straight, 2), 0.5);
			lineSplit2.addBehavior(new SplitBehavior(1, 80, 50, 0, straight, 0), 1.0);
			lineSplit2.addBehavior(new SplitBehavior(1, 80, 50, 0, straight, 1), 1.5);
			lineSplit2.addBehavior(new SplitBehavior(1, 80, 50, 0, straight, 2), 2.0);
			lineSplit2.addBehavior(new SplitBehavior(1, 80, 50, 0, straight, 0), 2.5);
			lineSplit2.addBehavior(new SplitBehavior(1, 80, 50, 0, straight, 1), 3.0);
			lineSplit2.addBehavior(new SplitBehavior(1, 80, 50, 0, straight, 2), 3.5);			
			lineSplit2.addBehavior(new SplitBehavior(1, 80, 50, 0, straight, 0), 4.0);
			lineSplit2.addBehavior(new SplitBehavior(1, 80, 50, 0, straight, 1), 4.5);
			lineSplit2.addBehavior(new AdjustSizeBehavior(2));
			
			var wavey:BulletPattern = new BulletPattern();
			wavey.addBehavior(new WaveBehavior(2, 3, 0, 0));
			
			var pattern1:BossPattern = new BossPattern();
			var p1t1:Turret = new Turret(0, 0, gravity, 0, -45, 100, 0.1);
			p1t1.addBehavior(new TypeCyclingTurretBehavior(0.7, [0, 1, 2]));
			p1t1.addBehavior(new SpinningTurretBehavior( -90, 90, true));
			
			var p1t2:Turret = new Turret(0, 0, reverseGravity, 0, 60, 150, 0.1);
			p1t2.addBehavior(new TypeCyclingTurretBehavior(0.7, [0, 1, 2]));
			p1t2.addBehavior(new SpinningTurretBehavior( 90, 60, true));			
			
			pattern1.addTurretTime(0, 8, p1t1);
			pattern1.addTurretTime(10, 16, p1t2);
			
			var pattern2:BossPattern = new BossPattern();
			var p2t1:Turret = new Turret(0, 0, lineSplit, 0, 120, 40, 1);
			p2t1.addBehavior(new TypeCyclingTurretBehavior(1.0, [2, 1, 0, 1, 2, 0, 2, 1, Bullet.CANDY]));
			var p2t2:Turret = new Turret(0, 0, lineSplit2, 0, 60, 50, 1);
			p2t2.addBehavior(new TypeCyclingTurretBehavior(1.0, [2, 1, 0]));
			
			var p2t3:Turret = new Turret(0, 0, stopAndDie, 0, 0, 70, 0.05);
			p2t3.addBehavior(new TypeCyclingTurretBehavior(0.4, [0, 1, 2]));
			p2t3.addBehavior(new SpinningTurretBehavior(720));
			
			pattern2.addTurretTime(0, 3, p2t3);
			pattern2.addTurretTime(3, 8, p2t1);
			pattern2.addTurretTime(8, 13, p2t2);
			
			var pattern3:BossPattern = new BossPattern();
			var p3t1:Turret = new Turret(-30, 0, wavey, 0, 70, 70, 0.3);
			p3t1.addBehavior(new TypeCyclingTurretBehavior(1.5, [0, 1, 2]));
			p3t1.addBehavior(new SpinningTurretBehavior(40, 40, true));
			
			var p3t2:Turret = new Turret(30, 0, wavey, 0, 110, 70, 0.3);
			p3t2.addBehavior(new TypeCyclingTurretBehavior(1.5, [1, 2, 0]));
			p3t2.addBehavior(new SpinningTurretBehavior(-40, 40, true));			
			
			pattern3.addTurretTime(0, Infinity, p3t1);
			pattern3.addTurretTime(0.15, Infinity, p3t2);
			
			
			var pattern4:BossPattern = new BossPattern();
			var p4t1:Turret = new Turret(0, 0, straight, Bullet.CANDY, 90, 30, 10);
			
			for (var i:int = 0; i < 9; i++)
			{
				var ang:Number = i * 6.2818 / 9;
				var tur:Turret = new Turret(Math.sin(ang) * 10, Math.cos(ang) * 10, straight, 2, 45, 120, 0.25);
				tur.addBehavior(new TypeCyclingTurretBehavior(0.25, [2, 0, 1, 0, 1, 2, 1, 0, 0, 2, 1]));
				tur.addBehavior(new SpinningTurretBehavior(600));
				pattern4.addTurretTime(2, Infinity, tur);
			}
			
			pattern4.addTurretTime(0.5, Infinity, p4t1);
			
			boss.addState(new BossState(1700, pattern4));
			boss.addState(new BossState(700, pattern1));
			boss.addState(new BossState(600, pattern2));
			boss.addState(new BossState(1400, pattern3));
			
			// Add candy in the first part (before pattern1) also tutorial.
		}
		
		override public function update():void 
		{
			super.update();
			if (!Saving.showedCandyTut && time < 1.5 && time + FlxG.elapsed >= 1.5)
			{
				showTutorial(img_tutcandy);
				Saving.showedCandyTut = true;
			}
		}			
	}

}